<?php

namespace App\Http\Controllers\common;

use GuzzleHttp\Client;
use \Exception;


class VoiceController
{
    protected $baseUri;
    protected $sname;
    protected $spwd;
    protected $sprdid;
    protected $scorpid;
    protected $client;

    /**
     * 设置账号密码 可不填
     * @param string $sname 账号
     * @param string $spwd 密码
     * @param string $scorpid 企业代码
     */
    public function __construct($sname='', $spwd='', $scorpid='')
    {
        $this->baseUri = 'http://cf.51welink.com';
        if (empty($sname)) {
            $this->sname = 'dlhnqykj';  
        } else { 
            $this->sname = $sname;
        }
        if (empty($spwd)) {
            $this->spwd = 'bjbj1234';
        }else{
            $this->spwd = $spwd;
        }
        if (empty($sprdid)) {
            $this->sprdid = '1012838';
        }else{
            $this->sprdid = $spwd;
        }
        if (empty($scorpid)) {
            $this->scorpid = '';
        } else {
            $this->scorpid = $scorpid;
        }
        $this->client = new Client(['base_uri'=>$this->baseUri]);
    }

    /**
    * @param 只可以 单条发送，或多号码相同内容发送 
    * @param string $phone 接收信息的手机号码。
    * @param string $cnt 发送的提交模板。
    * @param array $options 追加参数 选填。
    * @param string $options['certpath'] 追加参数 选填
    * @return json to string 原始返回值 State 为'0'时发送成功。
    */
    public function sendMsg ($phone, $cnt, $options=[])
    {
        $params = [
            'sdst' => $phone,
            'smsg' => $cnt
        ];
        if ( !empty($options) ) {
            $params = array_merge($params, $options);
        }
        try {
            $response = $this->send('/submitdata/Service.asmx/g_Submit', $params);
            return $response;
        } catch (Exception $e) {
          $msg = ['State' => '-1','MsgState' => '程序错误'];
          return json_encode($msg);
        }
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    protected function send($path , $params, $method='post')
    {
        if (isset($this->sname)) {
            $params['sname'] = $this->sname;
        }
        if (isset($this->spwd)) {
            $params['spwd'] = $this->spwd;
        }
        if (isset($this->sprdid)) {
            $params['sprdid'] = $this->sprdid;
        }
        if (isset($this->scorpid)) {
            $params['scorpid'] = $this->scorpid;
        }
        $method = strtolower($method);
        if(isset($params['certpath'])){
            $certpath = $params['certpath'];
        } else {
            $certpath = $_SERVER['DOCUMENT_ROOT'];
        }
        $cert = rtrim($certpath,'/').'/pc/cert.pem';
        if ($method == 'get') {
            $response = $this->client
                -> get(
                    $path,
                    [
                        'query' => $params,
                        'verify' => $cert
                    ]
                );
        }
        if ($method == 'post') {
            $response = $this->client
                -> post(
                    $path,
                    [
                        'form_params' => $params,
                        'verify' => $cert
                    ]
                );
        }
        $body = $response -> getBody() -> getContents();
        $postObj = simplexml_load_string(
                $body,
                'SimpleXMLElement',
                LIBXML_NOCDATA
            );
        $jsonStr = json_encode($postObj);
        $jsonArray = json_decode($jsonStr, true);
        return $jsonStr;
    }
}
